package com.example.bubble;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class BubbleView extends View {
    public BubbleView(Context context) {
        super(context);
    }

    public BubbleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BubbleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(16);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    invalidate();
                }
            }
        }).start();
    }

    int a = 1;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int maxY = canvas.getHeight();
        float amplitude = canvas.getWidth() / 2;
        float offset = a+=10;
        Paint paint = new Paint();
        paint.setStrokeWidth(4);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        for (float i = 0; i < maxY; i += 1f) {
            double x = Math.sin(Math.toRadians(i + offset)) * amplitude;
            canvas.drawPoint((int) ((x + amplitude + i / 20)), i, paint);
        }
    }
}
